﻿using System.Net;
using System.Text;

namespace RobosimAPI
{
	public static class Tools
	{
		public const string VERSION = "2.1.15";
		public static long GetTimestamp()
		{
			return (long)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
		}
		public static string GetMD5FromBytes(byte[] bytes)
		{
			using (var md5 = System.Security.Cryptography.MD5.Create())
			{
				byte[] hash = md5.ComputeHash(bytes);
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < hash.Length; i++)
				{
					sb.Append(hash[i].ToString("X2"));
				}
				return sb.ToString().ToLower();
			}
		}
		public static string GetMD5FromString(string? str)
		{
			using (var md5 = System.Security.Cryptography.MD5.Create())
			{
				byte[] data = Encoding.UTF8.GetBytes(str);
				byte[] hash = md5.ComputeHash(data);
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < hash.Length; i++)
				{
					sb.Append(hash[i].ToString("X2"));
				}
				return sb.ToString().ToLower();
			}
		}
		public static string GetMD5FromString16(string? str)
		{
			using (var md5 = System.Security.Cryptography.MD5.Create())
			{
				byte[] data = Encoding.UTF8.GetBytes(str);
				byte[] hash = md5.ComputeHash(data);
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < hash.Length; i++)
				{
					sb.Append(hash[i].ToString("X2"));
				}
				return sb.ToString().ToLower().Substring(8, 16);
			}
		}	
		public static string CreatToken(string? str)
		{
			Guid guid = Guid.NewGuid();
			string token = guid.ToString() + GetMD5FromString16(str);
			return token;
		}
		public static string GetSign(string token,string user_id,string date)
		{
			string sign = GetMD5FromString(GetMD5FromString(token + user_id + date));
            return sign;
		}
		public static string SignHttpPost(string url, string data, string time, string token,string user_id)
		{
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
			request.Method = "POST";
			request.ContentType = "application/x-www-form-urlencoded";
			byte[] bytes = Encoding.UTF8.GetBytes(data);
			request.ContentLength = bytes.Length;
			request.Headers.Add("Sign", GetSign(token, user_id, time));
			request.Headers.Add("Request-Date", time);
			using (Stream requestStream = request.GetRequestStream())
			{
				requestStream.Write(bytes, 0, bytes.Length);
			}
			using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
			{
				using (StreamReader reader = new StreamReader(response.GetResponseStream()))
				{
					return reader.ReadToEnd();
				}
			}
		}
		public static byte[] HttpGetBytes(string url)
		{
			// 创建 HttpWebRequest 对象
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
			request.Method = "GET";

			using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
			{
				// 获取响应流
				using (Stream stream = response.GetResponseStream())
				{
					// 将响应流转换为 byte[] 数据
					using (MemoryStream ms = new MemoryStream())
					{
						stream.CopyTo(ms);
						return ms.ToArray();
					}
				}
			}
		}
		public static void WriteToFile(string filePath, byte[] bytes)
		{
			// 获取文件的目录部分
			string directoryPath = Path.GetDirectoryName(filePath);

			// 检查目录是否存在，如果不存在则创建
			if (!Directory.Exists(directoryPath))
			{
				Directory.CreateDirectory(directoryPath);
			}

			// 使用 StreamWriter 写入文件
			File.WriteAllBytes(filePath, bytes);
		}
		public static string TimestampToTimeString(long timestamp)
		{
			DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
			DateTime dtEnd = dtStart.AddSeconds(timestamp);
			return dtEnd.ToString("yyyy-MM-dd HH:mm:ss");
		}
	}
	public class ErrorResponse
	{
		public int status { get; set; }
		public string? errmsg { get; set; }
	}
	public class SuccessResponse
	{
		public int status { get; set; }
		public object? data { get; set; }
	}
}
